package com.atguigu.bigdata.scala.chapter07;

public class Scala09_Collection_ThreadSafe_Java {
    public static void main(String[] args) {

        User09 user = new User09();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                user.name = "zhangsan";
                try {
                    Thread.sleep(1000);
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
                System.out.println(user.name);
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                user.name = "lisi";
                try {
                    Thread.sleep(1000);
                } catch ( Exception e ) {
                    e.printStackTrace();
                }
                System.out.println(user.name);
            }
        });

        // 启动线程，线程什么时候执行，不确定，因为需要靠JVM的线程调度机制决定的。
        t1.start();
        t2.start();

        // t1, main => main方法执行完毕, (sleep), zhangsan
        // main, t1 => main方法执行完毕, (sleep), zhangsan

        // t1, t2, main => main方法执行完毕, zhangsan, lisi
        // t1, main, t2 => main方法执行完毕, zhangsan, lisi
        // t2, t1, main => main方法执行完毕, lisi, zhangsan
        // t2, main, t1 => main方法执行完毕, lisi, zhangsan
        // main, t1, t2 => main方法执行完毕, zhangsan, lisi
        // main, t2, t1 => main方法执行完毕, lisi, zhangsan

        // main方法执行完毕, zhangsan, lisi
        // main方法执行完毕, lisi, zhangsan

        // 所谓的线程安全问题，其实是多线程并发执行时，对共享内存中共享对象的属性进行修改所导致的数据冲突问题
        //  main, lisi, lisi
        //  main, zhangsan, zhangsan
        System.out.println("main方法执行完毕");

    }
}

class User09 {

    public String name;
}
